package com.yq.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.reggie.common.BusinessException;
import com.yq.reggie.controller.Result;
import com.yq.reggie.mapper.CategoryMapper;
import com.yq.reggie.pojo.Category;
import com.yq.reggie.pojo.Dish;
import com.yq.reggie.pojo.Setmeal;
import com.yq.reggie.service.CategoryService;
import com.yq.reggie.service.DishService;
import com.yq.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;


    public Result remove(Long id){
        //判断是否有菜品
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,id);
        long count = dishService.count(wrapper);
        if(count>0){
            //抛出业务异常
            throw new BusinessException("该菜品分类中有菜品，删除失败");
        }
        //判断是否有套餐
        LambdaQueryWrapper<Setmeal> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Setmeal::getCategoryId,id);
        long count2 = setmealService.count(wrapper2);
        if(count2>0){
            //抛出业务异常
            throw new BusinessException("该菜品分类中有套餐，删除失败");
        }
        //删除分类
        super.removeById(id);
        return Result.success("删除分类成功");
    }
}
